๐Ÿ‘ฉ๐Ÿฟโ€๐ŸฆฐBasic Socket.io ํŠœํ† ๋ฆฌ์–ผ with Mysql (2)

๐Ÿ‘ฆ๐ŸปBasic Socket.io 2๋ถ€

broadcast ๋ผ๋“ ๊ฐ€ ์ด๋Ÿฐ ๋ถ€๋ถ„์€ ์ผ๋‹จ ์ƒ๋žต, 2๋ถ€์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ ์ƒ์— ๋ณด์—ฌ์ง€๊ฒŒ ํ•˜๊ณ  Mysql ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์™€ ์—ฐ๋™ํ•ด์„œ CRUD ๋ฅผ ์‹ค์ฒœํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.

(๋ฌผ๋ก  UD ๋Š” ๋‹ค์Œ์—)

15. ๋‘๋ฒˆ์งธ script ํƒœ๊ทธ ๋‚ด์— ํ•จ์ˆ˜ sendMessage ์ •์˜ํ•˜๊ธฐ

1ํŽธ์—์„œ form ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด ๋„ฃ์—ˆ๊ณ  onsubmit ์ด๋ฒคํŠธ๋ฅผ form ์— ๊ฑธ์—ˆ๋‹ค.

sendMessage() ํ•จ์ˆ˜๋ฅผ ์ด์ œ ์ •์˜ํ•ด ์ฃผ์ž.

์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •, ๊ธฐ์กด socket.emit ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜ ๋‚ด๋กœ ์œ„์น˜์‹œ์ผœ์„œ ๋ณ€๊ฒฝ ํ•˜๋„๋ก ํ•œ๋‹ค.

<script>
  const socket = io("http://localhost:3000");

  function sendMessage() {

    let message = document.querySelector("#message");

    // ! ์—ฐ์Šตํ–ˆ๋“ฏ์ด ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋‚ ๋ฆด๋•Œ๋Š” ๋ญ๋‹ค? emit!
    console.log(message.value);
    socket.emit("new_message", message.value);
    // ๋‹ค์‹œ ์„œ๋ฒ„ ์ชฝ์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ํด๋ผ์ด์–ธํŠธ์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ์ค€๋น„๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.

    return false;
    // this is prevent the form from submitting, ๋ฆฌ์•กํŠธ์—์„œ๋Š” e.preventDefault ๊ฐ€ ์žˆ๋“ฏ์ด
  }


  // client will listen from server
  socket.on("new_message", (data) => {
    // localhost:3000 ํŽ˜์ด์ง€ ๋‚ด ๊ฐœ๋ฐœ์ž ๋„๊ตฌ ์ฐฝ์—์„œ ํ™•์ธํ•ด ๋ณผ ๊ฒƒ
    console.log("Server says", data);
  }
</script>

6

16. server.js ์—์„œ socket ํด๋ผ์ด์–ธํŠธ ๋“ค์—๊ฒŒ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ

// To send same message back to all users
io.emit('new_message', 'Hello, Client๐Ÿ˜Š')

server.js ๋‚ด ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

io.emit('new_message', data)

ํด๋ผ์ด์–ธํŠธ์—์„œ ์ธํ’‹ ์ฐฝ์— ์ž…๋ ฅํ•œ ์ฑ„ํŒ…์ด data ๋ณ€์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋‹ด๊ฒจ ์„œ๋ฒ„๋กœ ์š”์ฒญ์ด ๋“ค์–ด๊ฐ”์œผ๋‹ˆ,

์ด์ œ ์ด data ๋ฅผ socket ์— ์ ‘์†ํ•œ ๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ ๋“ค์—๊ฒŒ ๋ฟŒ๋ ค์ฃผ๋Š” ๊ฑฐ๋‹ค.

ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €) ๊ฐœ๋ฐœ์ž๋„๊ตฌ๋ฅผ ๊ฐ€์„œ ํ™•์ธํ•ด ๋ณด๋ฉด ์ฑ„ํŒ…์ด ์ฐํžˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

7

17. li ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ „์†กํ•œ ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋“ค์ด ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ

index.html ์—์„œ

<script src="/socket.io/socket.io.js"></script>

<form onsubmit="return sendMessage()">
  <input id="message" placeholder="Enter message" />
  <input type="submit" value="Send" />
</form>

<!-- List where all messages will be displayed -->
<ul id="messages"></ul>

<script>
  ...์ดํ•˜ ์ƒ๋žต...

    // client will listen from server
    socket.on("new_message", (data) => {
      // localhost:3000 ํŽ˜์ด์ง€ ๋‚ด ๊ฐœ๋ฐœ์ž ๋„๊ตฌ ์ฐฝ์—์„œ ํ™•์ธํ•ด ๋ณผ ๊ฒƒ
      console.log("Server says", data);

      // display messages
      let li = document.createElement("li");

      li.innerHTML = data; // ๋งŒ๋“ค์–ด์ง„ li ๋‚ด ํ…์ŠคํŠธ๊ฐ€ server ์—์„œ ๋ฐ›์€ data ๋ฅผ ๋„ฃ๋Š”๋‹ค.
      const messages = document.querySelector("#messages");
      messages.appendChild(li); // ul ์— ์ž์‹์œผ๋กœ li ์ถ”๊ฐ€ํ•˜๊ธฐ
    });
</script>

18. ๋ธŒ๋ผ์šฐ์ € ํ…Œ์ŠคํŠธ

๋ธŒ๋ผ์šฐ์ € ํƒญ์„ ๋‘๊ฐœ ์ •๋„ ์—ด์–ด์„œ ์ธํ’‹์— ์ฑ„ํŒ…์„ ์ž…๋ ฅํ•˜๊ณ  submit ์„ ํ•จ๊ณผ ๋™์‹œ์— ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜๋˜๋Š” ๋ชจ์Šต์„ ํ™•์ธํ•ด ๋ณด์ž.

8

19. ๋Œ€๋ง์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ €์žฅํ•˜๊ธฐ, Saving all messages in database

๋จผ์ € mysql ์— ์ ‘์†ํ•ด์„œ โ€œsocketio_testโ€ ๋ผ๋Š” ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (๋งˆ์Œ๋Œ€๋กœ ์ง€์œผ์„ธ์š”)

create database socketio_test;

๊ทธ๋ฆฌ๊ณ  use socketio_test ๋กœ ๋“ค์–ด๊ฐ„ ํ›„, ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•œ๋‹ค.

create table messages
    (id INT(10) NOT NULL AUTO_INCREMENT,
    message VARCHAR(1024),
    PRIMARY KEY(id));

9

20. ๋””๋ ‰ํ† ๋ฆฌ root ๊ฒฝ๋กœ์—์„œ mysql ์„ค์น˜

npm install mysql

nodemon ์œผ๋กœ ์„œ๋ฒ„๋Š” ์ผœ๋‘˜๊ฒƒ

21. server.js ์—์„œ mysql ๊ด€๋ จ ๋ณ€์ˆ˜ ์„ค์ •, ์‚ฌ์šฉํ•˜๊ธฐ

io ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ๋ถ€๋ถ„ ์•„๋ž˜ ์ฏค์—๋‹ค ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

// use mysql
const mysql = require('mysql')

// create connection
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '1234',
  database: 'socketio_test',
})

connection.connect(error => {
  // show error, if any
})

์ด์ œ ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†กํ•ด์ค€ data (์ฑ—) ๋ถ€๋ถ„์„ ๋ด์•ผ ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†ก ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, database ์— ์ €์žฅ์„ ํ•ด์•ผ๊ฒ ์ง€?

socket.on('new_message', data => {
  console.log('Client says', data)

  // send same message back to all users
  io.emit('new_message', data)

  // ! save message in DATABASE
  connection.query(
    "INSERT INTO messages (message) VALUES ('" + data + "')",
    function(error, result) {
      //
    }
  )
})

์ž ์ด์ œ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•ด๋ณด์ž. ๊ทธ๋ฆฌ๊ณ  ์ธํ’‹์ฐฝ์— ์ž…๋ ฅํ›„ ์—”ํ„ฐ ์ณ๋ณด์ž.

๊ทธ ๋‹ค์Œ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ํ™•์ธํ•ด ๋ณด์ž. ๋“ค์–ด๊ฐ€๊ณ  ์žˆ๋‹ค.

10

22. ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋”ฉ ๋  ๋•Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋˜์—ˆ๋˜ ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋“ค์„ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ํ•˜๊ธฐ

๋จผ์ € index.html ๋กœ ๊ฐ€์„œ ๋‘ ๋ฒˆ์งธ ์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ ๋‚ด sendMessage ํ•จ์ˆ˜ ์ฝ”๋“œ์˜ ์•„๋ž˜ ์ฏค์— ajax ์š”์ฒญ์„ ๋„ฃ์„ ์ฐจ๋ก€๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ componentDidMount, useEffect ์— ๋„ฃ์„ ๋น„๋™๊ธฐ ์š”์ฒญ (fetch) ๋“ฑ์ด ์žˆ๊ฒ ๋‹ค.

์š”์ฒญ์„ ํ•˜๋ ค๋ฉด? ๋ฐฑ์—”๋“œ์—์„œ์˜ ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค.

์ผ๋‹จ, fetch(โ€http://localhost:3000/get_messagesโ€) ์ •๋„๋กœ ์ž‘์„ฑํ•ด์ฃผ๊ณ  ๋‹ค์‹œ server.js ๋กœ ๋Œ์•„๊ฐ€์•ผ ํ•œ๋‹ค.

์ฒ˜์Œ์— ์ž‘์„ฑํ•œ ์‹ฌํ”Œ get request ์ฝ”๋“œ ์œ„์ฏค์— ์ž‘์„ฑํ•œ๋‹ค.

// ! create API for get_message
app.get('/get_messages', (req, res) => {
  connection.query('SELECT * FROM messages', (error, messages) => {
    // return data will be in JSON format
    res.end(JSON.stringify(messages))
    // ์ด๊ฑฐ ํ•˜๊ธฐ ์‹ซ์œผ๋ฉด app.use(express.json()); ๋ฅผ ์ ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.
  })
})

๊ทธ๋Ÿผ ์ด์ œ index.html ๋กœ ๋Œ์•„๊ฐ€์„œ ํด๋ผ์ด์–ธํŠธ์˜ fetch ์š”์ฒญ์„ ๋งˆ์ € ์ž‘์„ฑํ•œ๋‹ค.

fetch('http://localhost:3000/get_messages')
  .then(res => res.json())
  .then(data => {
    const messages = document.querySelector('#messages')
    for (let i = 0; i < data.length; i++) {
      // display message, creates new DOM element
      let li = document.createElement('li')

      // give it unique id (in order to delete message)
      li.id = data[i].id

      // add message content as HTML
      li.innerHTML = data[i].message

      // append at the end of list
      messages.appendChild(li)
    }
  })

๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ƒˆ๋กœ๊ณ ์นจ ํ•ด๋ณด์ž.

์ด์ œ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ƒˆ๋กœ ์—ด์–ด๋„ ํ•ด๋‹น ์ฑ„ํŒ…์˜ ๋‚ด์šฉ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ณ ์Šค๋ž€ํžˆ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋˜ํ•œ ๋ณต์ˆ˜์˜ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด ๋ณด๋ฉด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋‚˜์˜ ์ฑ„ํŒ…์ด ๋‹ค๋ฅธ ์œ ์ €๋“ค์—๊ฒŒ (ํด๋ผ์ด์–ธํŠธ) ๋ฐ˜์˜๋˜๋Š” ๊ฒƒ ๋˜ํ•œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

11

์ถ”๊ฐ€๋กœ delete ํ•˜๋Š” ๊ฒƒ๋„ ๋„ฃ์œผ๋ ค ํ–ˆ์œผ๋‚˜ ์ด๊ฑด ๋‚˜์ค‘์— ๋„ฃ๊ฒ ๋‹ค.

์ผ๋‹จ ์ด๊ฑธ ์ดํ•ดํ•˜๋ฉด socket.io ์˜ ์Œฉ๊ธฐ์ดˆ ์ •๋„๋Š” ๋ง›์„ ๋ดค๋‹ค ์ •๋„๊ฐ€ ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค.

๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก๐Ÿคก


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook